package cn.yusei.teamTribe.service.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.JedisDAO;
import cn.yusei.teamTribe.dao.user.UserAuthDAO;
import cn.yusei.teamTribe.util.JedisKeyUtil;
import cn.yusei.teamTribe.util.SmsUtil;

@Service
public class PhoneSmsService {
	@Autowired
	UserAuthDAO userAuthDAO;
	@Autowired
	private JedisDAO jedisDAO;
	
	@Value("${sid}")
	private String sid;
	@Value("${token}")
	private String token;
	@Value("${appid}")
	private String appid;
	@Value("${templateid}")
	private String templateid;
	@Value("${tryAginWaitSecond}")
	private int tryAginWaitSecond;
	@Value("${validateSecond}")
	private int validateSecond;
	
	public int getSms(String phone,String validateCode){
		if(jedisDAO.get(JedisKeyUtil.PHONE_WAITING + phone) != null)
			return ResponseCode.TRY_AGIN_WAITING;
		if(userAuthDAO.selectPhone(phone) != null )
			return ResponseCode.PHONE_ALREADY_EXISTS;
        SmsUtil.sendSms(sid, token, appid, templateid, validateCode, phone, "");
        jedisDAO.setex(JedisKeyUtil.PHONE_WAITING + phone, tryAginWaitSecond, "");
        jedisDAO.setex(JedisKeyUtil.PHONE_CODE + phone, validateSecond, validateCode);
        return ResponseCode.REQUEST_SUCCESSED;
	}

}
